import useUserStore from '@/store/modules/user'
import tool from '@/utils/tool';

/**
 * 字符权限校验
 * @param {Array} value 校验值
 * @returns {Boolean}
 */
export function checkPermi(value) {
  if (value && value instanceof Array && value.length > 0) {
    const permissions = useUserStore().permissions
    const permissionDatas = value
    const all_permission = "*:*:*"

    const hasPermission = permissions.some(permission => {
      return all_permission === permission || permissionDatas.includes(permission)
    })

    if (!hasPermission) {
      return false
    }
    return true
  } else {
    console.error(`need roles! Like checkPermi="['system:user:add','system:user:edit']"`)
    return false
  }
}

/**
 * 角色权限校验
 * @param {Array} value 校验值
 * @returns {Boolean}
 */
export function checkRole(value) {
  if (value && value instanceof Array && value.length > 0) {
    const roles = useUserStore().roles
    const permissionRoles = value
    const super_admin = "admin"

    const hasRole = roles.some(role => {
      return super_admin === role || permissionRoles.includes(role)
    })

    if (!hasRole) {
      return false
    }
    return true
  } else {
    console.error(`need roles! Like checkRole="['admin','editor']"`)
    return false
  }
}

// /**
//  * 是否含有不限分类，有则表示全部允许通过
//  */
// export function permissionAll() {
//   const allPermissions = "*/*/*"
//   let permissions = tool.data.get("PERMISSIONS");
//   return permissions.includes(allPermissions);
// }

/**
 * 比对两组数据是否一致
 * @param news
 * @param old
 * @returns {boolean}
 */
export function judementSameArr(news, old) {
  // console.log(news)
  // console.log(old)
  let count = 0;
  const leng = news.length;
  for (let i in news) {
    for (let j in old) {
      if (news[i] === old[j]) {
        count++;
        // console.log(news[i])
      }
    }
  }
  // console.log('相同的数量', count)
  return count === leng;
}

// export function permission(data) {
//   let permissions = tool.data.get("PERMISSIONS");
//   if (!permissions) {
//     return false;
//   }
//   let isHave = permissions.includes(data);
//   return isHave;
// }

// export function rolePermission(data) {
//   let userInfo = tool.data.get("USER_INFO");
//   if (!userInfo) {
//     return false;
//   }
//   let role = userInfo.role;
//   if (!role) {
//     return false;
//   }
//   let isHave = role.includes(data);
//   return isHave;
// }
